package com.xudow.app.ui;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.base.AgencySchools;
import com.activeshare.edu.ucenter.models.base.AgencySchoolsWithInfo;
import com.xudow.app.R;
import com.xudow.app.data.CourseData;
import com.xudow.app.data.Pair;
import com.xudow.app.ui.adapter.AgencySchoolsAdapter;
import com.xudow.app.ui.adapter.CourseLocationAdapter;
import com.xudow.app.ui.task.AgencyschoolsTask;

import java.util.List;

/**
 * 课程区域选择弹出窗口
 *
 * @author lukecheng
 */
public class CourseLocationPopupWindow extends PopupWindow {
    private Context context;

    private View contentView;
    private TextView cancelTextView;
    private ListView locationListView;

    private List<Pair> areas;
    private CourseLocationAdapter courseLocationAdapter;

    private String areaValue;

    private int flag=-1;//1加载指定学校校区（目前只有大帝学校）,-1加载城市区域
    private List<AgencySchools> agencyshoolslist;
    private AgencySchoolsAdapter agencyschooladapter;
    private CourseLocationListener courseLocationListener;


    public CourseLocationPopupWindow(Context context, String areaValue, final int flag,CourseLocationListener listener, List<AgencySchools> data) {
        super(context);
        this.flag=flag;
        this.context = context;
        this.areaValue = areaValue;
        this.agencyshoolslist=data;
        courseLocationListener = listener;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.layout_course_location_popup_window, null);

        setContentView(contentView);

        cancelTextView = (TextView) contentView.findViewById(R.id.cancel_action);
        cancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        locationListView = (ListView) contentView.findViewById(R.id.locations);


        Log.e("CourseLocationPopupWin","flag="+flag );
        if (flag==-1) {
            areas = CourseData.areas();

            for (Pair p : areas) {
                if (p.second.equals(areaValue)) {
                    p.checked = true;
                    break;
                }
            }
            courseLocationAdapter = new CourseLocationAdapter(context, android.R.layout.simple_list_item_1, areas);
            locationListView.setAdapter(courseLocationAdapter);

            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setFocusable(true);
            setAnimationStyle(R.style.CourseFilterPopupAnimation);

            ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.translucent));
            setBackgroundDrawable(dw);

        }
        //TODO 加载大帝校区列表
        if (flag==1){
            agencyschooladapter = new AgencySchoolsAdapter(context, agencyshoolslist);
            locationListView.setAdapter(agencyschooladapter);

            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setFocusable(true);
            setAnimationStyle(R.style.CourseFilterPopupAnimation);

            ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.translucent));
            setBackgroundDrawable(dw);
        }
        locationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (flag==-1) {
                    courseLocationListener.onLocationSelected(areas.get(position));
                } else if (flag==1){
                    AgencySchools item=agencyshoolslist.get(position);
                    for (AgencySchools as:agencyshoolslist) {
                        as.selected=false;
                    }
                    item.selected=true;
                    courseLocationListener.onLocationSelected(item);
                }
                dismiss();

            }
        });
    }

    public interface CourseLocationListener {
        /**
         * 区域选项选择事件
         *
         * @param pair
         */
        void onLocationSelected(Pair pair);
        void onLocationSelected(AgencySchools as);
    }
}

